package com.ease.platform.usercontext.util;

import com.ease.platform.model.type.IdentityUser;
import com.ease.platform.usercontext.vo.UserContext;

/*
 http://spring.group.iteye.com/group/blog/815998

 @Transient和transient关键字在hibernate中和memcache中应用

 将字段标识为transient可以使得POJO的这个字段不被映射到数据库中的字段,但是如果要将这个POJO存入memcache就会无法存储(如果这个POJO是实现的Serializable,而不是Externalizable接口自己写的序列化方面实现)。
 只有将字段用注解@Transient的方式标志,就能实现不映射到数据库,同时又能序列化到memcache中。
 如下代码：
 Java代码
 private transient ProjectInfoState projectInfoState;


 @Transient
 private ProjectInfoState projectInfoState;
 */
public class UserContextThreadLocal {// extends Detect

	private static final ThreadLocal<UserContext> threadLocal = new ThreadLocal<UserContext>();

	public static UserContext getUserContext() {
		return threadLocal.get();
	}

	public static void setUserContext(UserContext userContext) {
		threadLocal.set(userContext);
	}

	public static void remove() {
		threadLocal.remove();
	}

	public static IdentityUser getUser() {
		UserContext userContext = threadLocal.get();
		if (null == userContext) {
			return null;
		}
		return userContext.getUser();
	}

	public static String getName() {
		UserContext userContext = threadLocal.get();
		if (null == userContext || null == userContext.getUser()) {
			return "";
		}
		return userContext.getUser().getName();
	}

	public static long getUserId() {
		UserContext userContext = threadLocal.get();
		if (null == userContext || null == userContext.getUser()) {
			return 0;
		}
		return userContext.getUser().getId();
	}

}
